package com.longqinuo.mycarservice.adapter;


import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

import com.longqinuo.mycarservice.R;
import com.longqinuo.mycarservice.entity.KeepServer;
import com.longqinuo.mycarservice.utils.PathUtils;

import java.util.List;


/**
 * Created by zxw on 2016/5/24.
 */
public class KeepServerAdapter extends RecyclerView.Adapter<KeepServerAdapter.MyViewHolder>{
    private List<KeepServer> mList;
    private int itemLayout;
    private OnItemClickListener onItemClickListener;
    private ImageLoader mImageLoader;
    public KeepServerAdapter(List<KeepServer> mList, int itemLayout,ImageLoader imageLoader) {
        this.mList = mList;
        this.itemLayout = itemLayout;
        this.mImageLoader = imageLoader;
    }
    public interface OnItemClickListener {
        void onItemClick(View itemView, int position);
    }
    public void setOnItemClickListener (OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(),itemLayout,null);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        KeepServer k = mList.get(position);
        String imgs = k.getImage();
        holder.image.setImageUrl(PathUtils.SERVER_PHONE+imgs,mImageLoader);

        holder.name.setText(k.getName());
        holder.address.setText(k.getCom_location());
        holder.currentPrice.setText("￥"+k.getPrice());
        holder.oldPrice.setText("￥"+k.getOriginaPrice());
        holder.orderCount.setText(k.getCount()+"人已预约");

        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(View itemView) {
            super(itemView);
            this.image = (NetworkImageView) itemView.findViewById(R.id.iv_keep_img);
            this.name = (TextView) itemView.findViewById(R.id.tv_keep_name);
            this.address = (TextView) itemView.findViewById(R.id.tv_address_keep);
            this.currentPrice = (TextView) itemView.findViewById(R.id.tv_keep_current_price);
            this.oldPrice = (TextView) itemView.findViewById(R.id.tv_keep_old_price);
            this.orderCount = (TextView) itemView.findViewById(R.id.tv_order_count_keep);
        }
        public NetworkImageView image;
        public TextView name;
        public TextView address;
        public TextView currentPrice;
        public TextView oldPrice;
        public TextView orderCount;

    }
}

